#ifndef TETRIS_GAME_HPP
#define TETRIS_GAME_HPP

#include "constants.hpp"
#include "board.hpp"
#include "piece.hpp"
#include <ncurses.h>
#include <chrono>
#include <random>

namespace Tetris {

class Game {
public:
    Game();
    ~Game();

    void run(); // 游戏主循环

private:
    void setupNcurses();
    void teardownNcurses();
    void initColors();

    void newGame();
    void generateNewPiece();

    void handleInput(int ch);
    void updateLogic();
    void render();

    // 辅助绘制幽灵方块
    void drawGhostPiece(WINDOW* win, int offset_x, int offset_y);
    // 辅助绘制下一个方块预览
    void drawNextPiece(WINDOW* win, int offset_x, int offset_y);

    // 辅助绘制分数/等级
    void drawScoreInfo(WINDOW* win, int offset_x, int offset_y);
         
    Board m_board;
    Piece* m_currentPiece;
    Piece* m_nextPiece;

    bool m_gameOver;
    bool m_gamePaused;
    long m_score;
    int m_level;
    long m_fallIntervalMs;

    // 计时器
    std::chrono::high_resolution_clock::time_point m_lastFallTime;

    // 随机数生成器
    std::mt19937 m_rng;
    std::uniform_int_distribution<int> m_pieceDist;

    // ncurses窗口
    WINDOW* m_gameWin;
    WINDOW* m_nextPieceWin;
    WINDOW* m_scoreWin;
};

} // namespace Tetris

#endif // TETRIS_GAME_HPP
